﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using CSSEVLD;
using Csla;

namespace CommonSLT
{
    public class CSSEDetailForm : DevExpress.XtraEditors.XtraForm
    {
        #region protected variants
        protected string formText = "";
        protected string addTilte = "Thêm";
        protected string copyTilte = "Sao Chép";
        protected string editTilte = "Sửa";
        protected FormStatus formStatus;
        protected bool wasChanged = false;
        #endregion protected variants
        //==========================================================================

        #region public variants
        public event OnSaveCompletedEventHandler OnSaveCompleted = null;
        public int currIndex = 0;
        public int[] idCollection = null;
        #endregion public variants
        //==========================================================================

        #region control
        public DevExpress.XtraEditors.PanelControl pnContent2;
        public DevExpress.XtraEditors.PanelControl pnButton;
        private IContainer components;
        public SimpleButton btnSaveAndClose;
        public SimpleButton btnReset;
        public SimpleButton btnClose;
        public BindingSource bindingSource;
        public DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxError = new DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider();
        public DevExpress.XtraEditors.PanelControl pnContent;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.pnButton = new DevExpress.XtraEditors.PanelControl();
            this.btnSaveAndClose = new DevExpress.XtraEditors.SimpleButton();
            this.btnClose = new DevExpress.XtraEditors.SimpleButton();
            this.btnReset = new DevExpress.XtraEditors.SimpleButton();
            this.pnContent = new DevExpress.XtraEditors.PanelControl();
            this.pnContent2 = new DevExpress.XtraEditors.PanelControl();
            this.bindingSource = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pnButton)).BeginInit();
            this.pnButton.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnContent)).BeginInit();
            this.pnContent.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnContent2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSource)).BeginInit();
            this.SuspendLayout();
            // 
            // pnButton
            // 
            this.pnButton.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnButton.Controls.Add(this.btnSaveAndClose);
            this.pnButton.Controls.Add(this.btnClose);
            this.pnButton.Controls.Add(this.btnReset);
            this.pnButton.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pnButton.Location = new System.Drawing.Point(0, 153);
            this.pnButton.Name = "pnButton";
            this.pnButton.Size = new System.Drawing.Size(393, 32);
            this.pnButton.TabIndex = 1;
            // 
            // btnSaveAndClose
            // 
            this.btnSaveAndClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnSaveAndClose.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnSaveAndClose.Appearance.Options.UseFont = true;
            this.btnSaveAndClose.Image = global::CommonSLT.Properties.Resources.Save;
            this.btnSaveAndClose.Location = new System.Drawing.Point(132, 3);
            this.btnSaveAndClose.Name = "btnSaveAndClose";
            this.btnSaveAndClose.Size = new System.Drawing.Size(75, 26);
            this.btnSaveAndClose.TabIndex = 0;
            this.btnSaveAndClose.Text = "Lưu";
            this.btnSaveAndClose.ToolTip = "Đóng cửa sổ sau khi Lưu thành công";
            // 
            // btnClose
            // 
            this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnClose.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnClose.Appearance.Options.UseFont = true;
            this.btnClose.Image = global::CommonSLT.Properties.Resources.Exit;
            this.btnClose.Location = new System.Drawing.Point(315, 3);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(75, 26);
            this.btnClose.TabIndex = 2;
            this.btnClose.Text = "Đóng";
            // 
            // btnReset
            // 
            this.btnReset.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnReset.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnReset.Appearance.Options.UseFont = true;
            this.btnReset.Image = global::CommonSLT.Properties.Resources.Reset;
            this.btnReset.Location = new System.Drawing.Point(213, 3);
            this.btnReset.Name = "btnReset";
            this.btnReset.Size = new System.Drawing.Size(96, 26);
            this.btnReset.TabIndex = 1;
            this.btnReset.Text = "Thiết Đặt Lại";
            this.btnReset.ToolTip = "Trả lại giá trị ban đầu";
            // 
            // pnContent
            // 
            this.pnContent.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnContent.Controls.Add(this.pnContent2);
            this.pnContent.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnContent.Location = new System.Drawing.Point(0, 0);
            this.pnContent.Name = "pnContent";
            this.pnContent.Padding = new System.Windows.Forms.Padding(3);
            this.pnContent.Size = new System.Drawing.Size(393, 153);
            this.pnContent.TabIndex = 4;
            // 
            // pnContent2
            // 
            this.pnContent2.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
            this.pnContent2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnContent2.Location = new System.Drawing.Point(3, 3);
            this.pnContent2.Name = "pnContent2";
            this.pnContent2.Size = new System.Drawing.Size(387, 147);
            this.pnContent2.TabIndex = 4;
            // 
            // CSSEDetailForm
            // 
            this.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Appearance.Options.UseFont = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
            this.ClientSize = new System.Drawing.Size(393, 185);
            this.Controls.Add(this.pnContent);
            this.Controls.Add(this.pnButton);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimumSize = new System.Drawing.Size(300, 100);
            this.Name = "CSSEDetailForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "CSSEDetailForm";
            this.Load += new System.EventHandler(this.CSSEDetailForm_Load);
            this.Shown += new System.EventHandler(this.CSSEDetailForm_Shown);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CSSEDetailForm_FormClosing);
            ((System.ComponentModel.ISupportInitialize)(this.pnButton)).EndInit();
            this.pnButton.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnContent)).EndInit();
            this.pnContent.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnContent2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSource)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion control
        //==========================================================================

        #region form
        public CSSEDetailForm()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(CSSEDetailForm_KeyDown);
            this.btnSaveAndClose.Click += new EventHandler(btnSaveAndClose_Click);
            this.btnReset.Click += new EventHandler(btnReset_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
        }

        #region form events
        private void CSSEDetailForm_Load(object sender, EventArgs e)
        {
            try
            {
                InitForm();
                InitControlEventChanging();
                switch (this.formStatus)
                {
                    case FormStatus.Add:
                    case FormStatus.QuickAdd:
                        this.Text = addTilte + " " + this.formText;
                        break;
                    case FormStatus.Copy:
                        this.Text = copyTilte + " " + this.formText;
                        break;
                    case FormStatus.Edit:
                        this.Text = editTilte + " " + this.formText;
                        break;
                }
                InitData();
                InitBO();
            }
            catch (Exception ex)
            {
                MyDialog.Show("Lỗi:\r\nCSSEDetailForm_Load\r\n" + ex.Message, "Lỗi", MyDialogButtons.OK, MyDialogIcon.Error);
            }
        }

        private void CSSEDetailForm_Shown(object sender, EventArgs e)
        {
            try
            {
                SetControlFocus();
                this.wasChanged = (this.formStatus == FormStatus.Copy);
            }
            catch (Exception ex)
            {
                MyDialog.Show("Lỗi:\r\nCSSEDetailForm_Shown\r\n" + ex.Message, "Lỗi", MyDialogButtons.OK, MyDialogIcon.Error);
            }
        }

        private void CSSEDetailForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.wasChanged)
                {
                    MyDialogResult dgResult = MyDialog.Show("Bạn có muốn lưu lại trước khi thoát không?", "Lưu ý", MyDialogButtons.YesNoCancel, MyDialogIcon.Question);
                    if (dgResult == MyDialogResult.Yes)
                    {
                        bool saveSuccessful = false;
                        try
                        {
                            saveSuccessful = SaveAction();
                        }
                        catch
                        {
                        }
                        if (saveSuccessful == false)
                            e.Cancel = true;
                    }
                    else
                        if (dgResult == MyDialogResult.Cancel)
                            e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                MyDialog.Show("Lỗi khi đóng:\r\n" + ex.Message, "Lỗi", MyDialogButtons.OK, MyDialogIcon.Error);
            }
        }

        private void CSSEDetailForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                btnClose_Click(null, null);
            else
                if (e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control)
                    btnSaveAndClose_Click(null, null);
                else
                    if (e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control)
                        btnReset_Click(null, null);
        }
        #endregion form events
        #endregion form
        //==========================================================================

        #region virtual function
        protected virtual void InitForm()
        {
        }

        protected virtual void InitBO()
        {
        }

        protected virtual void InitControlEventChanging()
        {
        }

        protected virtual void InitData()
        {
        }

        protected virtual void SetControlFocus()
        {
        }

        protected virtual object GetObjectValue()
        {
            return null;
        }

        protected virtual bool VLD()
        {
            return true;
        }

        protected virtual void Save()
        {
        }
        #endregion virtual function
        //==========================================================================

        #region protected function
        protected void SetControlKeyValue(Control ctrl)
        {
            string controlType = ctrl.GetType().ToString();
            switch (controlType)
            {
                case "DevExpress.XtraEditors.CheckedComboBoxEdit":
                    CheckedComboBoxEdit chkCmbEdit = (CheckedComboBoxEdit)ctrl;
                    chkCmbEdit.Properties.ValueMember = "Key";
                    chkCmbEdit.Properties.DisplayMember = "Value";
                    chkCmbEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
                case "DevExpress.XtraEditors.ComboBoxEdit":
                    ComboBoxEdit cmbEdit = (ComboBoxEdit)ctrl;
                    cmbEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
                case "DevExpress.XtraEditors.DateEdit":
                    DateEdit dateEdit = (DateEdit)ctrl;
                    dateEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
                case "DevExpress.XtraEditors.LookUpEdit":
                    LookUpEdit lookUpEdit = (LookUpEdit)ctrl;
                    lookUpEdit.Properties.ValueMember = "Key";
                    lookUpEdit.Properties.DisplayMember = "Value";
                    lookUpEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
                case "DevExpress.XtraEditors.ImageComboBoxEdit":
                    ImageComboBoxEdit cmbImg = (ImageComboBoxEdit)ctrl;
                    cmbImg.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
            }
        }

        protected void SetControlKeyValue(Control ctrl, string valueMember, string displayMember)
        {
            string controlType = ctrl.GetType().ToString();
            switch (controlType)
            {
                case "DevExpress.XtraEditors.CheckedComboBoxEdit":
                    CheckedComboBoxEdit chkCmbEdit = (CheckedComboBoxEdit)ctrl;
                    chkCmbEdit.Properties.ValueMember = valueMember;
                    chkCmbEdit.Properties.DisplayMember = displayMember;
                    chkCmbEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
                case "DevExpress.XtraEditors.LookUpEdit":
                    LookUpEdit lookUpEdit = (LookUpEdit)ctrl;
                    lookUpEdit.Properties.ValueMember = valueMember;
                    lookUpEdit.Properties.DisplayMember = displayMember;
                    lookUpEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    break;
            }
        }

        protected void SetControlEventChanging(Control ctrl)
        {
            string controlType = ctrl.GetType().ToString();
            switch (controlType)
            {
                case "DevExpress.XtraEditors.CheckedComboBoxEdit":
                    CheckedComboBoxEdit chkCmbEdit = (CheckedComboBoxEdit)ctrl;
                    chkCmbEdit.Properties.ValueMember = "Key";
                    chkCmbEdit.Properties.DisplayMember = "Value";
                    chkCmbEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    chkCmbEdit.EditValueChanged += new EventHandler(EditValueChanged);
                    break;
                case "DevExpress.XtraEditors.CheckEdit":
                    CheckEdit chkEdit = (CheckEdit)ctrl;
                    chkEdit.CheckedChanged += new EventHandler(chkEdit_CheckedChanged);
                    break;
                case "DevExpress.XtraEditors.ComboBoxEdit":
                    ComboBoxEdit cmbEdit = (ComboBoxEdit)ctrl;
                    cmbEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    cmbEdit.TextChanged += new EventHandler(cmbEdit_TextChanged);
                    break;
                case "DevExpress.XtraEditors.DateEdit":
                    DateEdit dateEdit = (DateEdit)ctrl;
                    dateEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    dateEdit.EditValueChanged += new EventHandler(EditValueChanged);
                    break;
                case "DevExpress.XtraEditors.LookUpEdit":
                    LookUpEdit lookUpEdit = (LookUpEdit)ctrl;
                    lookUpEdit.Properties.ValueMember = "Key";
                    lookUpEdit.Properties.DisplayMember = "Value";
                    lookUpEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    lookUpEdit.EditValueChanged += new EventHandler(lookUpControlEventChanged);
                    break;
                case "DevExpress.XtraEditors.MemoEdit":
                    MemoEdit memoEdit = (MemoEdit)ctrl;
                    memoEdit.TextChanged += new EventHandler(memoEdit_TextChanged);
                    break;
                case "DevExpress.XtraEditors.PictureEdit":
                    PictureEdit picEdit = (PictureEdit)ctrl;
                    picEdit.EditValueChanged += new EventHandler(EditValueChanged);
                    break;
                case "DevExpress.XtraEditors.SpinEdit":
                    SpinEdit spinEdit = (SpinEdit)ctrl;
                    spinEdit.EditValueChanged += new EventHandler(EditValueChanged);
                    break;
                case "DevExpress.XtraEditors.TextEdit":
                    TextEdit textEdit = (TextEdit)ctrl;
                    textEdit.TextChanged += new EventHandler(textEdit_TextChanged);
                    break;
                case "DevExpress.XtraEditors.TimeEdit":
                    TimeEdit timeEdit = (TimeEdit)ctrl;
                    timeEdit.EditValueChanged += new EventHandler(EditValueChanged);
                    break;
                case "DevExpress.XtraEditors.ImageComboBoxEdit":
                    ImageComboBoxEdit cmbImg = (ImageComboBoxEdit)ctrl;
                    cmbImg.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    cmbImg.EditValueChanged += new EventHandler(EditValueChanged);
                    break;
            }
        }

        protected bool VLDByCtrl(Control ctrl, VLDData vldData, string sError)
        {
            bool isPass = true;
            MyVLDReturn myVLDReturn = VLDUtils.VLDUtil(vldData);
            isPass = (myVLDReturn == MyVLDReturn.OK);
            if (isPass)
                this.dxError.SetError(ctrl, "");
            else
                this.dxError.SetError(ctrl, sError);
            return isPass;
        }

        protected bool VLDByCtrl(Control ctrl, VLDData[] vldDatas, string[] sErrors)
        {
            bool isPass = true;
            for (int i = 0; i < vldDatas.Length; i++)
            {
                isPass &= VLDByCtrl(ctrl, vldDatas[i], sErrors[i]);
                if (!isPass)
                    break;
            }
            return isPass;
        }

        protected bool VLDByCtrl(Control ctrl, VLDData vldData)
        {
            bool isPass = true;
            MyVLDReturn myVLDReturn = VLDUtils.VLDUtil(vldData);
            isPass = (myVLDReturn == MyVLDReturn.OK);
            if (isPass)
                this.dxError.SetError(ctrl, "");
            else
            {
                string sError = "";
                switch (myVLDReturn)
                {
                    case MyVLDReturn.ChkMaxFailed:
                        sError = "Giá trị không được lớn hơn " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.ChkMinFailed:
                        sError = "Giá trị không được nhỏ hơn " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.ChkMinMaxFailed:
                        sError = "Giá trị phải nằm trong khoảng từ " + vldData.myValue[1] + " " + vldData.myValue[2];
                        break;
                    case MyVLDReturn.IsNotDate:
                        sError = "Không phải ngày tháng (Ngày/Tháng/Năm)";
                        break;
                    case MyVLDReturn.IsNotDateTime:
                        sError = "Không phải ngày giờ (Ngày/Tháng/Năm Giờ:Phút:Giây)";
                        break;
                    case MyVLDReturn.IsNotEqual:
                        sError = "Giá trị phải là " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.IsNotEqualGreater:
                        sError = "Giá trị phải lớn hơn hoặc bằng " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.IsNotEqualSmaller:
                        sError = "Giá trị phải nhỏ hơn hoặc bằng " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.IsNotFloat:
                        sError = "Không phải số thực";
                        break;
                    case MyVLDReturn.IsNotGreater:
                        sError = "Giá trị phải lớn hơn " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.IsNotInt:
                        sError = "Không phải số nguyên";
                        break;
                    case MyVLDReturn.IsNotNull:
                        sError = "Không được nhập dữ liệu";
                        break;
                    case MyVLDReturn.IsNotNumber:
                        sError = "Không phải là số";
                        break;
                    case MyVLDReturn.IsNotSmaller:
                        sError = "Giá trị phải nhỏ hơn " + vldData.myValue[1];
                        break;
                    case MyVLDReturn.IsNotString:
                        sError = "Không phải ký tự";
                        break;
                    case MyVLDReturn.IsNotTime:
                        sError = "Không phải giờ (Giờ:Phút:Giây)";
                        break;
                    case MyVLDReturn.IsNull:
                        sError = "Chưa nhập dữ liệu";
                        break;
                    case MyVLDReturn.ChkPasswordFailed:
                        sError = "Mật khẩu không hợp lệ.\r\n- Chỉ chứa chữ cái và chữ số.\r\n- Độ dài từ 6-30 ký tự.";
                        break;
                    case MyVLDReturn.ChkRegularFailed:
                        sError = "Giá trị không hợp lệ";
                        break;
                    case MyVLDReturn.ChkSpecialCharFailed:
                        sError = "Không được chứa các ký tự đặc biệt:\r\n- Các ký tự điều khiển.\r\n- Các ký tự: ` ~ ! @ # % ^ * '.";
                        break;
                    case MyVLDReturn.ChkUsernameFailed:
                        sError = "Tên đăng nhập không hợp lệ.\r\n- Bắt đầu bằng chữ cái hoặc ký tự gạch dưới.\r\n- Chỉ chứa các ký tự, chữ số và ký tự gạch dưới.";
                        break;
                }
                this.dxError.SetError(ctrl, sError);
            }
            return isPass;
        }

        protected bool VLDByCtrl(Control ctrl, VLDData[] vldDatas)
        {
            bool isPass = true;
            for (int i = 0; i < vldDatas.Length; i++)
            {
                isPass &= VLDByCtrl(ctrl, vldDatas[i]);
                if (!isPass)
                    break;
            }
            return isPass;
        }
        #endregion protected function
        //==========================================================================

        #region private function
        private bool SaveAction()
        {
            if (VLD())
            {
                Save();
                OnSaveCompletedEventArgs evt = new OnSaveCompletedEventArgs(this.formStatus, GetObjectValue());
                if (this.OnSaveCompleted != null)
                    this.OnSaveCompleted(this, evt);
                switch (this.formStatus)
                {
                    case FormStatus.Add:
                        InitBO();
                        break;
                }
                this.wasChanged = false;
                if (this.formStatus != FormStatus.Add)
                    CloseForm();
                SetControlFocus();
                if (this.formStatus == FormStatus.Add)
                    MyDialog.Show("Thêm dữ liệu thành công!", "Thông báo", MyDialogButtons.OK, MyDialogIcon.Info);

                return true;
            }
            else
                return false;
        }

        private void Reset()
        {
            this.dxError.ClearErrors();
            InitBO();
            SetControlFocus();
        }

        private void CloseForm()
        {
            this.Close();
        }
        #endregion private function
        //==========================================================================

        #region events
        private void textEdit_TextChanged(object sender, EventArgs e)
        {
            try
            {
                TextEdit textEdit = (TextEdit)sender;
                this.dxError.SetError(textEdit, "");
                if (textEdit.Text == "")
                    textEdit.EditValue = null;
                ChangedData();
            }
            catch { }
        }

        private void memoEdit_TextChanged(object sender, EventArgs e)
        {
            try
            {
                MemoEdit memoEdit = (MemoEdit)sender;
                this.dxError.SetError(memoEdit, "");
                if (memoEdit.Text == "")
                    memoEdit.EditValue = null;
                ChangedData();
            }
            catch { }
        }

        private void cmbEdit_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ComboBoxEdit cmbEdit = (ComboBoxEdit)sender;
                this.dxError.SetError(cmbEdit, "");
                if (cmbEdit.Text == "")
                    cmbEdit.EditValue = null;
                ChangedData();
            }
            catch { }
        }

        private void chkEdit_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckEdit chkEdit = (CheckEdit)sender;
                this.dxError.SetError(chkEdit, "");
                ChangedData();
            }
            catch { }
        }

        private void lookUpControlEventChanged(object sender, EventArgs e)
        {
            try
            {
                LookUpEdit lookUpEdit = (LookUpEdit)sender;
                try
                {
                    if ((int)lookUpEdit.EditValue == -1)
                        lookUpEdit.EditValue = null;
                }
                catch { }
                this.dxError.SetError(lookUpEdit, "");
                ChangedData();
            }
            catch { }
        }

        private void EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.dxError.SetError((Control)sender, "");
                ChangedData();
            }
            catch { }
        }

        private void ChangedData()
        {
            this.wasChanged = true;
        }

        private void btnSaveAndClose_Click(object sender, EventArgs e)
        {
            try
            {
                SaveAction();
            }
            catch (Exception ex)
            {
                MyDialog.Show("Lỗi khi lưu:\r\n" + ex.Message, "Lỗi", MyDialogButtons.OK, MyDialogIcon.Error);
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                Reset();
                this.wasChanged = false;
            }
            catch (Exception ex)
            {
                MyDialog.Show("Lỗi:\r\nbtnReset_Click\r\n" + ex.Message, "Lỗi", MyDialogButtons.OK, MyDialogIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                CloseForm();
            }
            catch (Exception ex)
            {
                MyDialog.Show("Lỗi khi đóng:\r\n" + ex.Message, "Lỗi", MyDialogButtons.OK, MyDialogIcon.Error);
            }
        }
        #endregion events
        //==========================================================================
    }
}